package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {

    @Value("${pagepath}")
    private String pagepath;

    @Autowired
    private TemplateEngine templateEngine;
    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private CategoryFeign categoryFeign;

    @Autowired
    private SkuFeign skuFeign;

    @Override
    public void generatePage(String spuId) {
        //1.获取context对象，用于存放商品详情数据
        Context context = new Context();
        Map<String,Object> itemData = this.getItemData(spuId);
        context.setVariables(itemData);
        //2.获取商品页面存储位置
        File dir = new File(pagepath);
        if (!dir.exists()){
            dir.mkdirs();
        }
        //3.判断指定存储文件夹是否存在，如果不存在则生成
        if (!dir.exists()){
            dir.mkdirs();
        }
        //4.定义输出流，完成页面生成
        File file = new File(dir + "/" + spuId + ".html");
        Writer out = null;
        try{
            out = new PrintWriter(file);
            //生成静态化页面
            /**
             * 1.模板名称
             * 2.数据内容
             * 3.输出流
             */
            templateEngine.process("item",context,out);
        }catch (Exception e){
            e.printStackTrace();
        }finally {

            //5.关闭流
            try {
                out.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

    private Map<String,Object> getItemData(String spuId){
        Map<String,Object> resultMap = new HashMap<>();

        //获取spu数据
        Result<Spu> spu = spuFeign.findSpuById(spuId);
        resultMap.put("spu",spu.getData());
        //获取spu图片数据
        resultMap.put("imageList",spu.getData().getImages());

        //获取spu分类数据
        Result<Category> category1 = categoryFeign.findById(spu.getData().getCategory1Id());
        resultMap.put("category1",category1);

        Result<Category> category2 = categoryFeign.findById(spu.getData().getCategory2Id());
        resultMap.put("category2",category2);

        Result<Category> category3 = categoryFeign.findById(spu.getData().getCategory3Id());
        resultMap.put("category3",category3);

        //获取sku相关信息
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        resultMap.put("skuList",skuList);

        //获取商品规格信息
        resultMap.put("specificationList", JSON.parseObject(spu.getData().getSpecItems(),Map.class));
        return resultMap;
    }
}
